package com.orange.controller.manage;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.orange.controller.BaseController;
import com.orange.handler.AuthSubjectService;
import com.orange.modules.entity.manage.AuthSubject;
import com.orange.util.ReturnCode;

@Controller
@RequestMapping("/subject")
public class AuthSubjectController extends BaseController{

	private static final Logger logger = Logger.getLogger(AuthSubjectController.class);
	
	@Autowired
	private AuthSubjectService authSubjectService;
	
	/**
	 * 保存学科
	 * @param request
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/saveAuthSubjectInfo" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> saveAuthSubjectInfo(HttpServletRequest request,@RequestBody Map<String,String> map){
		String name = map.get("name");
		String taskId = map.get("taskId");
		if(name == null || taskId == null || StringUtils.isBlank(name) || StringUtils.isBlank(taskId))
			return toResponse(null, ReturnCode.FAIL, "请填写完整");
		AuthSubject as = new AuthSubject();
		as.setName(name);
		as.setTaskId(taskId);
		as.setState("1");
		authSubjectService.saveAuthSubject(as);
		return toResponse(as, ReturnCode.SUCCESS, "添加成功!");	
	}
	
	/**
	 * 查询学科
	 */
	@RequestMapping(value = "/queryAuthSubjectInfo" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> queryAuthSubjectInfo(HttpServletRequest request,@RequestBody Map<String,String> map){
		String taskId = map.get("taskId");
		if(taskId == null || StringUtils.isBlank(taskId))
			return toResponse(null, ReturnCode.FAIL, "参数不合法!");
		try {
			List<AuthSubject> as = authSubjectService.queryAuthSubjectByTaskId(taskId);
			if(as == null)
				return toResponse(null, ReturnCode.SUCCESS, "请先添加学科!");
			return toResponse(as, ReturnCode.SUCCESS, "查询成功!");
		} catch (SQLException e) {
			logger.error(e);
			return toResponse(null, ReturnCode.FAIL, "查询失败!");
		}
	}
	
	/**
	 * 查询所有学科
	 */
	@RequestMapping(value = "/queryAllAuthSubject" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> queryAllAuthSubject(HttpServletRequest request,@RequestBody Map<String,String> map){
		try {
			List<AuthSubject> as = authSubjectService.queryAllAuthSubject();
			if(as == null)
				return toResponse(null, ReturnCode.SUCCESS, "请先添加学科!");
			return toResponse(as, ReturnCode.SUCCESS, "查询成功!");
		} catch (SQLException e) {
			logger.error(e);
			return toResponse(null, ReturnCode.FAIL, "查询失败!");
		}
	}
}
